package com.hnluchuan.staff.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.hnluchuan.staff.common.Sex;
import com.hnluchuan.staff.dto.EducationDTO;
import com.hnluchuan.staff.model.Education;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.staff.dto.EmployeeDTO;
import com.hnluchuan.staff.service.EmployeeService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.web.controller.BaseController;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/admin/employee", produces="text/html;charset=UTF-8")
public class AdminEmployeeController extends BaseController {

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			EmployeeDTO dto = employeeService.load(id);
			map.put("n", dto);
		}
		return "admin/employee/employee_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(EmployeeDTO dto) throws Throwable{
		if (dto.getId() == null) {
			employeeService.create(dto);
		} else  {
			EmployeeDTO old = employeeService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			/*old.setUser(dto.getUser());*/
			old.setId(dto.getId());
			old.setUser_id(dto.getUser_id());
			old.setPosition(dto.getPosition());
			old.setName(dto.getName());
			old.setBirth(dto.getBirth());
			old.setSex(dto.getSex());
			old.setHeight(dto.getHeight());
			old.setWeight(dto.getWeight());
			old.setPlace(dto.getPlace());
			old.setNation(dto.getNation());
			old.setPhone(dto.getPhone());
			old.setIDCard(dto.getIDCard());
			old.setSchool(dto.getSchool());
			old.setMajor(dto.getMajor());
			old.setEducation(dto.getEducation());
			old.setLocation(dto.getLocation());
			old.setAddress(dto.getAddress());
			old.setWechat(dto.getWechat());
			old.setEmail(dto.getEmail());
			old.setMarry(dto.getMarry());
			old.setInTime(dto.getInTime());
			old.setStatus(dto.getStatus());

			old.setEducations(dto.getEducations());
			old.setEmergencyContact(dto.getEmergencyContact());
			old.setExperience(dto.getExperience());
			old.setFamily(dto.getFamily());


			/*判断性别和身份证是否一致*/
			int sex=dto.getSex();
			int idSex=Integer.parseInt(String.valueOf((dto.getIDCard()).charAt(16)));
			if((sex==1&&idSex%2!=1)||(sex==2&&idSex%2!=0)){
			    throw new Exception("身份证与性别不一致，请核对");
            }
			else {
                employeeService.updateAllFields(old);
            }

		}
		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(HttpServletResponse response, Long[] ids){
		employeeService.deleteByIds(ids);
		return ok();
	}
	
	@RequestMapping(value = "/list")
	public String list(EmployeeDTO dto, Boolean search, Map<String, Object> map, Page page) {
		List<EmployeeDTO> dtos = employeeService.find(dto, page);
		/*map.put("id的值",对象)*/
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/employee/employee_list";
	}
	
	@RequestMapping(value = "/search")
	public String search(EmployeeDTO dto, Map<String, Object> map, Page page) {
		List<EmployeeDTO> dtos = employeeService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/employee/employee_search";
	}
	
}
